<!--
 * @Descripttion: (/AlarmDust)
 * @version: (1.0)
 * @Author: (admin)
 * @Date: (2024-11-08)
 * @LastEditors: (admin)
 * @LastEditTime: (2024-11-08)
-->
<template>
  <div class="app-container">
    <!-- :model属性用于表单验证使用 比如下面的el-form-item 的 prop属性用于对表单值进行验证操作 -->
    <el-form :model="queryParams" size="small" label-position="right" inline ref="queryForm" :label-width="labelWidth" v-show="showSearch" 
      @submit.native.prevent>
      <el-form-item label="主机名称" prop="machineName">
          <el-select v-model="queryParams.machineName" clearable filterable placeholder="请输入主机名称">
            <el-option v-for="item in hostOptions" :key="item.id" :label="item.machineName" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主机地址" prop="location">
          <el-input v-model="queryParams.location" placeholder="请输入主机地址" />
        </el-form-item>
        <el-form-item label="主机场景" prop="machineSence">
          <el-input v-model="queryParams.machineSence" placeholder="请输入主机场景" />
        </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="exportExcel">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据区域 -->
    <el-table :data="dataList" v-loading="loading" ref="table" border highlight-current-row @sort-change="sortChange" @selection-change="handleSelectionChange">
      <!-- <el-table-column type="selection" width="50" align="center"/> -->
      <!-- <el-table-column prop="id" label="Id" align="center" /> -->
      <el-table-column prop="machineName" label="主机名称" align="center" width="140"/>
      <el-table-column prop="machineSence" label="主机场景" align="center" />
      <el-table-column prop="alarmType" label="报警类型 " align="center">
       
      </el-table-column>
     
      <el-table-column prop="alarmValue" label="报警数据" align="center"  :show-overflow-tooltip="true" />
      <el-table-column prop="location" label="位置" align="center" :show-overflow-tooltip="true" />
   
      <el-table-column prop="alarmInfo" label="报警信息" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="saveTime" label="保存时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">     
          <el-button size="mini" v-hasPermi="['alarmdust:delete']" type="danger" icon="el-icon-delete" title="删除" 
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination class="mt10" background :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
<!-- 打印数据区域 -->
<el-table :data="dataList2" border id="printTable" v-if="false">
      <!-- <el-table-column type="selection" width="50" align="center"/> -->
      <!-- <el-table-column prop="id" label="Id" align="center" /> -->
      <el-table-column prop="machineName" label="主机名称" align="center" width="140"/>
      <el-table-column prop="machineSence" label="主机场景" align="center" />
      <el-table-column prop="alarmType" label="报警类型 " align="center">
       
      </el-table-column>
     
      <el-table-column prop="alarmValue" label="报警数据" align="center"  :show-overflow-tooltip="true" />
      <el-table-column prop="location" label="位置" align="center" :show-overflow-tooltip="true" />
   
      <el-table-column prop="alarmInfo" label="报警信息" align="center" :show-overflow-tooltip="true" />
      <el-table-column prop="saveTime" label="保存时间" align="center" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" width="140">
        <template slot-scope="scope">     
          <el-button size="mini" v-hasPermi="['alarmdust:delete']" type="danger" icon="el-icon-delete" title="删除" 
            @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>

import FileSaver from 'file-saver'
import * as XLSX from 'xlsx'
import { 
  listAlarmDust,
} from '@/api/dust/alarmDust.js';

export default {
  name: "alarmdust",
  data() {
    return {
      hostParams: {
        pageNum: 1,
        pageSize: 100,
        machineName:"",
        location:"",
        machineSence:"",
      },
      hostOptions: [],
      labelWidth: "100px",
      formLabelWidth:"100px",
      // 选中id数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 遮罩层
      loading: false,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        machineName:"",
        location:"",
        machineSence:"",
      },
      queryPrintParams: {
        pageNum: 1,
        pageSize: 10,
        machineName:"",
        location:"",
        machineSence:"",
      },
      // 弹出层标题
      title: "",
      // 操作类型 1、add 2、edit
      opertype: 0,
      // 报警类型 选项列表 格式 eg:{ dictLabel: '标签', dictValue: '0'}
      alarmTypeOptions: [],
      // 数据列表
      dataList: [],
      dataList2: [],
      // 总记录数
      total: 0,
    };
  },
  created() {    
    // 列表数据查询
    this.getList();
    this.listHostMachine(this.hostParams).then(res => {
      this.hostOptions = res.data.result;
    });
    
  },
  methods: {
    /**
         * 导出表格数据
         * @returns {any}
         */
         exportExcel() {
          this.queryPrintParams.pageNum="1",
          this.queryPrintParams.pageSize=this.total;
          this.queryPrintParams.machineName=this.queryParams.machineName;
          this.queryPrintParams.location=this.queryParams.location;
          this.queryPrintParams.machineSence=this.queryParams.machineSence;
          listAlarmDust(this.queryPrintParams).then(res => {
         if (res.code == 200) {
           this.dataList2 = res.data.result;
           const et = XLSX.utils.table_to_book(document.getElementById('printTable'))
          // 此处传入table元素节点，XLSX.utils.table_to_book对传入的数据进行加工，赋值给et
          // XLSX.write() 开始编写Excel表格
          const etout = XLSX.write(et, {
            bookType: 'xlsx', bookSST: true, type: 'array'
          })
          try {
            FileSaver.saveAs(new Blob([etout], {
              type: 'application/octet-stream'
            }), '导出表格名称.xlsx')
          } catch (e) {
            this.$message.error('导出失败')
          }
          return etout
         }
       })


        },
    // 查询数据
    getList() {
      this.loading = true;
      listAlarmDust(this.queryParams).then(res => {
         if (res.code == 200) {
           this.dataList = res.data.result;
           this.total = res.data.totalNum;
           this.loading = false;
         }
       })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 重置数据表单
    reset() {
      this.form = {
        id: undefined,
        alarmType: undefined,
        hostId: undefined,
        alarmValue: undefined,
        location: undefined,
        saveTime: undefined,
        alarmInfo: undefined,
      };
      this.resetForm("form");
    },
    // 重置查询操作
    resetQuery() {
      this.timeRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length != 1
      this.multiple = !selection.length;
    },
     // 自定义排序
    sortChange(column) {
      if (column.prop == null || column.order == null) {
        this.queryParams.sort = undefined;
        this.queryParams.sortType = undefined;
      } else {
        this.queryParams.sort = column.prop;
        this.queryParams.sortType = column.order;
      }

      this.handleQuery();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
};
</script>